package com.whfc.wxmp.api.sys.controller;

import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.Apply.rep.*;
import com.whfc.entity.dto.Apply.req.*;
import com.whfc.env.dto.GasDetectionQueryReq;
import com.whfc.env.dto.GasDetectionResultDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.mach.entity.AppMach;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.wxmp.api.sys.dto.ConfirmRequestDTO;
import com.whfc.wxmp.api.sys.service.WxWorkPlanService;
import com.whfc.wxmp.dto.WorkPlanDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author xjy
 * @description 谢江泳
 * @date 2025-09-13
 * 工作计划
 */
@RestController
@RequestMapping("/mp/api/workPlan")
public class WxWorkPlanController {

    @Autowired
    private WxWorkPlanService wxWorkPlanService;

    /**
     * 获取审核计划列表
     * @return
     */
    @GetMapping("/work/list")
    public Result queryReviewList(@CurrentWxUser WxUser user,
                                  AppEntryApplicationReq request) {
        List<WorkPlanDTO> institutions = wxWorkPlanService.entryReview(user, request);
        return ResultUtil.success(institutions);
    }

    /**
     * 添加工作计划
     *
     * @return 描述申请操作响应的字符串
     */
    @PostMapping("/saveWorkPlan")
    public Result saveWorkPlan(@CurrentWxUser WxUser user, @RequestBody WorkPlanCreatePlanReq req) {
        String msg = wxWorkPlanService.saveWorkPlan(user, req);
        if(!Objects.equals(msg, "提交成功")){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    /**
     * 获取入场Id
     * @return
     */
    @GetMapping("/queryEntryerId")
    public Result queryEntryerId(@CurrentWxUser WxUser user) {
        if(user.getId()==null){
            return ResultUtil.failure("当前用户未登录,请重新登录!");
        }
        return ResultUtil.success(wxWorkPlanService.queryEntryerId(user));
    }

    /**
     * 获取施工人員
     * @param id 入场凭证id
     * @return
     */
    @GetMapping("/queryWorkers")
    public Result queryWorkers(Integer id) {
        if(id == null){
            return ResultUtil.failure("请选择入场凭证!");
        }
        List<SafetyTrainingExamReq> application = wxWorkPlanService.queryConstructionWorkers(id);
        return ResultUtil.success(application);
    }

    /**
     * 获取工作区域
     * @param deptId 项目Id
     * @return
     */
    @GetMapping("/queryWorkArea")
    public Result queryWorkArea(@RequestParam("deptId") Integer deptId) {
        if(deptId == null){
            return ResultUtil.failure("项目id不能为空");
        }
        List<RegionRep> application = wxWorkPlanService.queryWorkArea(deptId);
        return ResultUtil.success(application);
    }

    /**
     * 获取设备清单
     * @param req
     * @return
     */
    @PostMapping("/queryMach")
    public Result queryMach(@RequestBody EquipmentIdReq req) {
        List<AppMach> application = wxWorkPlanService.queryMach(req);
        return ResultUtil.success(application);
    }

    /**
     * 获取到场确认
     * @return
     */
    @PostMapping("/submit")
    public Result submitConfirm(@CurrentWxUser WxUser user,@RequestBody ConfirmRequestDTO requestDTO) {
        return ResultUtil.success(wxWorkPlanService.confirm(user,requestDTO));
    }

    /**
     * 获取到场确认列表
     * @return
     */
    @PostMapping("/submit/list")
    public Result submitConfirmList(@CurrentWxUser WxUser user,@RequestBody ConfirmRequestDTO requestDTO) {
        return ResultUtil.success(wxWorkPlanService.confirmList(user,requestDTO));
    }

    /**
     * 审核
     *
     * @param id
     * @return
     */
    @GetMapping("/review")
    public Result review(@CurrentWxUser WxUser user, @RequestParam("approvalResult") Integer approvalResult, @RequestParam("id") Integer id) {
        String review = wxWorkPlanService.review(user,approvalResult, id);
        if(Objects.equals(review,"审核成功!")){
            return ResultUtil.success(review);
        }else {
            return ResultUtil.failure(review);
        }
    }

    /**
     * 获取最新气体信息
     * @param queryReq
     * @return
     */
    @PostMapping("/gasDetection")
    public Result getGasDetectionData(@RequestBody GasDetectionQueryReq queryReq) {
        GasDetectionResultDTO result = wxWorkPlanService.getGasDetectionData(queryReq);
        return ResultUtil.success(result);
    }

}
